<?php
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use app\common\model as M;

/**
 * 销售自定义提醒
 */
class Noticecustomer extends Command
{
    protected function configure()
    {
        $this->setName('noticecustomer')
            ->setDescription('销售自定义提醒');
    }

    /**
     * 计划任务
     */  
    protected function execute(Input $input, Output $output)
    {
        $today = date('Y-m-d');

        $tasks = M\CustomerNotice::where('notice_date' , $today)->where('process_time',0)->order('notice_time ASC')->select();

        $time  = date('H:i:s');

        foreach ( $tasks as $task ) {
            ## 脆弱时间到，我们一起来祷告
            if ( strtotime($time) >= strtotime($task->notice_time) ) {
                $res = M\Message::saveOne([
                    'title'         => '您有新的自定义提醒',
                    'summary'       => $task->notice_content,
                    'content'       => $task->notice_content,
                    'mtype'         => M\Message::MSG_TYPE_CUSTOMER,
                    'mgap_days'     => 1,
                    'uid'           => $task->uid,
                    'cycle'         => 1,
                    'send_time'     => date('Y-m-d H:i:s'),
                ]);
                if ( $res === false ) continue;

                M\CustomerNotice::where('id' , $task->id)->update(['process_time' => time()]);
            }
        }
    }
}// Class End